﻿#region Apache License
/* 
   Copyright 2011 Jorge Teixeira

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace AdHoc.Wavesque.Test
{
    [TestFixture(Description = "Unit tests for class Squaresque.")]
    public class SquaresqueTest
    {
        [Test(Description = "Create an object representation of a square wave.")]
        public void Create()
        {
            var f = new Squaresque(SquaresqueTestCaseData.Step, SquaresqueTestCaseData.Amplitude, SquaresqueTestCaseData.Frequency, SquaresqueTestCaseData.Phase);
            Assert.IsInstanceOf(typeof(Squaresque), f);
        }

        [Test(Description = "Create an object representation of a square wave.")]
        public void Create2()
        {
            var f = new Squaresque(SquaresqueTestCaseData.Amplitude, SquaresqueTestCaseData.Frequency, SquaresqueTestCaseData.Phase);
            Assert.IsInstanceOf(typeof(Squaresque), f);
        }

        [Test(Description = "Creates a synchronized object representation of a square wave.")]
        public void Create3()
        {
            var f = Squaresque.Synchronized(new Squaresque(SquaresqueTestCaseData.Amplitude, SquaresqueTestCaseData.Frequency, SquaresqueTestCaseData.Phase));
            Assert.IsInstanceOf(typeof(Squaresque), f);
            Assert.IsTrue(f.IsSynchronized);
        }

        [Test(Description = "Generate a default number of points of a square wave.")]
        public void GetValuesTest()
        {
            var f = new Squaresque(SquaresqueTestCaseData.Amplitude, SquaresqueTestCaseData.Frequency, SquaresqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.GetValues(out x, out y);

            Assert.AreEqual(100, y.Length);
        }

        [Test(Description = "Generate a default number of points of a square wave.")]
        public void GetValuesTest2()
        {
            var f = new Squaresque(SquaresqueTestCaseData.Amplitude, SquaresqueTestCaseData.Frequency, SquaresqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.GetValues(out x, out y, SquaresqueTestCaseData.X0);

            Assert.AreEqual(100, y.Length);
        }

        [Test(Description = "Generate a given number of points of a square wave.")]
        public void GetValuesTest3()
        {
            var f = new Squaresque(SquaresqueTestCaseData.Amplitude, SquaresqueTestCaseData.Frequency, SquaresqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.GetValues(SquaresqueTestCaseData.NumberOfPoints, out x, out y);

            Assert.AreEqual(SquaresqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a given number of points of a square wave.")]
        public void GetValuesTest4()
        {
            var f = new Squaresque(SquaresqueTestCaseData.Amplitude, SquaresqueTestCaseData.Frequency, SquaresqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.GetValues(SquaresqueTestCaseData.NumberOfPoints, out x, out y, SquaresqueTestCaseData.X0);

            Assert.AreEqual(SquaresqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a given number of points of a square wave.")]
        public void GetValuesTest5()
        {
            var f = Squaresque.Synchronized(new Squaresque(SquaresqueTestCaseData.Amplitude, SquaresqueTestCaseData.Frequency, SquaresqueTestCaseData.Phase));

            Assert.IsTrue(f.IsSynchronized);

            double[] x;
            double[] y;
            f.GetValues(SquaresqueTestCaseData.NumberOfPoints, out x, out y, SquaresqueTestCaseData.X0);

            Assert.AreEqual(SquaresqueTestCaseData.NumberOfPoints, y.Length);
        }
        
        [Test(Description = "Generate a given number of points of a square wave."), TestCaseSource(typeof(SquaresqueTestCaseData), "Data")]
        public void GetValuesTest(double step, double amplitude, double frequency, double phase, double x0, uint numberOfPoints)
        {
            double[] aX;
            double[] aY;

            var f = new Squaresque(step, amplitude, frequency, phase);
            f.GetValues(numberOfPoints, out aX, out aY, x0);
            
            Assert.AreEqual(numberOfPoints, aY.Length);
            
            List<double> bX = new List<double>();
            List<double> bY = new List<double>();
                        
            foreach(double pos in aX)
            {
            	double[] x;
            	double[] y;
            	var g = new Squaresque(step, amplitude, frequency, phase);
            	g.GetValues(1, out x, out y, pos);
            	
            	Assert.AreEqual(1, x.Length);
            	Assert.AreEqual(1, y.Length);
            	
            	bX.AddRange(x);
            	bY.AddRange(y);
            }
            
            Assert.AreEqual(aX, bX);
            Assert.AreEqual(aY, bY);
        }
        
        [Test(Description = "Generate a given number of points of a square wave, half wave rectification.")]
        public void ToHalfWaveRectification()
        {
            var f = new Squaresque(SquaresqueTestCaseData.Amplitude, SquaresqueTestCaseData.Frequency, SquaresqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToHalfWaveRectification(SquaresqueTestCaseData.NumberOfPoints, out x, out y);

            Assert.AreEqual(SquaresqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a given number of points of a square wave, half wave rectification.")]
        public void ToHalfWaveRectification2()
        {
            var f = new Squaresque(SquaresqueTestCaseData.Amplitude, SquaresqueTestCaseData.Frequency, SquaresqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToHalfWaveRectification(SquaresqueTestCaseData.NumberOfPoints, out x, out y, SquaresqueTestCaseData.X0);

            Assert.AreEqual(SquaresqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a default number of points of a square wave, half wave rectification.")]
        public void ToHalfWaveRectification3()
        {
            var f = new Squaresque(SquaresqueTestCaseData.Amplitude, SquaresqueTestCaseData.Frequency, SquaresqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToHalfWaveRectification(out x, out y);

            Assert.AreEqual(100, y.Length);
        }

        [Test(Description = "Generate a default number of points of a square wave, half wave rectification.")]
        public void ToHalfWaveRectification4()
        {
            var f = new Squaresque(SquaresqueTestCaseData.Amplitude, SquaresqueTestCaseData.Frequency, SquaresqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToHalfWaveRectification(out x, out y, SquaresqueTestCaseData.X0);

            Assert.AreEqual(100, y.Length);
        }

        [Test(Description = "Generate a given number of points of a square wave, full wave rectification.")]
        public void ToFullWaveRectification()
        {
            var f = new Squaresque(SquaresqueTestCaseData.Amplitude, SquaresqueTestCaseData.Frequency, SquaresqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToFullWaveRectification(SquaresqueTestCaseData.NumberOfPoints, out x, out y);

            Assert.AreEqual(SquaresqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a given number of points of a square wave, full wave rectification.")]
        public void ToFullWaveRectification2()
        {
            var f = new Squaresque(SquaresqueTestCaseData.Amplitude, SquaresqueTestCaseData.Frequency, SquaresqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToFullWaveRectification(SquaresqueTestCaseData.NumberOfPoints, out x, out y, SquaresqueTestCaseData.X0);

            Assert.AreEqual(SquaresqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a given number of points of a square wave, full wave rectification.")]
        public void ToFullWaveRectification3()
        {
            var f = new Squaresque(SquaresqueTestCaseData.Amplitude, SquaresqueTestCaseData.Frequency, SquaresqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToFullWaveRectification(out x, out y);

            Assert.AreEqual(100, y.Length);
        }

        [Test(Description = "Generate a default number of points of a square wave, full wave rectification.")]
        public void ToFullWaveRectification4()
        {
            var f = new Squaresque(SquaresqueTestCaseData.Amplitude, SquaresqueTestCaseData.Frequency, SquaresqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToFullWaveRectification(out x, out y, SquaresqueTestCaseData.X0);

            Assert.AreEqual(100, y.Length);
        }
    }
}
